﻿Imports Aricie.DNN.Modules.LuceneSearch.Business
Imports Ventrian.NewsArticles
Imports DotNetNuke.Services.Search
Imports DotNetNuke.Entities.Modules
Imports DotNetNuke.Common.Utilities
Imports System.Web

Namespace Aricie.DNN.Modules.LuceneSearch.ModuleProviders

    Public Class VentrianNewsArticleLatestProvider
        Implements ILuceneFieldGlossary
        Implements ISearchable

#Region "Implement ILuceneFieldGlossary"
        Public Function GetFieldDefinitions() As System.Collections.Generic.IList(Of Business.FieldDefinition) Implements ILuceneFieldGlossary.GetFieldDefinitions
            Dim toReturn As New List(Of FieldDefinition)
            Return toReturn
        End Function
#End Region

#Region "Implement ISearchable"
        Public Function GetSearchItems(ByVal ModInfo As DotNetNuke.Entities.Modules.ModuleInfo) As DotNetNuke.Services.Search.SearchItemInfoCollection Implements DotNetNuke.Entities.Modules.ISearchable.GetSearchItems
            Dim objArticleController As New ArticleController
            Dim searchItemsCollect As New SearchItemInfoCollection

            Dim objController As New DotNetNuke.Entities.Modules.ModuleController()
            Dim Settings As Hashtable = objController.GetModuleSettings(ModInfo.ModuleID)

            Dim articleModuleID As Integer = Convert.ToInt32(objController.GetTabModuleSettings(ModInfo.TabModuleID)(ArticleConstants.LATEST_ARTICLES_MODULE_ID).ToString())

            Dim pageNumber As Integer = 1

            Dim cats As Integer() = Nothing

            Dim showRelated As Boolean = False
            If (Settings.Contains(ArticleConstants.LATEST_ARTICLES_SHOW_RELATED)) Then
                showRelated = Convert.ToBoolean(Settings(ArticleConstants.LATEST_ARTICLES_SHOW_RELATED).ToString())
            End If

            'Récupération des paramètrages
            If (showRelated = False) Then
                If (Settings.Contains(ArticleConstants.LATEST_ARTICLES_CATEGORIES)) Then
                    If Not (Settings(ArticleConstants.LATEST_ARTICLES_CATEGORIES).ToString = Null.NullString Or Settings(ArticleConstants.LATEST_ARTICLES_CATEGORIES).ToString = "-1") Then
                        Dim categories As String() = Settings(ArticleConstants.LATEST_ARTICLES_CATEGORIES).ToString().Split(Char.Parse(","))

                        Dim categoriesToDisplay(categories.Length - 1) As Integer
                        For i As Integer = 0 To categories.Length - 1
                            categoriesToDisplay(i) = Convert.ToInt32(categories(i))
                        Next

                        cats = categoriesToDisplay
                    End If
                End If
            End If

            Dim matchOperator As MatchOperatorType = MatchOperatorType.MatchAny
            If (Settings.Contains(ArticleConstants.LATEST_ARTICLES_MATCH_OPERATOR)) Then
                matchOperator = CType(System.Enum.Parse(GetType(MatchOperatorType), Settings(ArticleConstants.LATEST_ARTICLES_MATCH_OPERATOR).ToString()), MatchOperatorType)
            End If

            Dim matchAll As Boolean = False
            If (matchOperator = MatchOperatorType.MatchAll) Then
                matchAll = True
            End If

            Dim catsExclude As Integer() = Nothing

            If (Settings.Contains(ArticleConstants.LATEST_ARTICLES_CATEGORIES_EXCLUDE)) Then
                If Not (Settings(ArticleConstants.LATEST_ARTICLES_CATEGORIES_EXCLUDE).ToString = Null.NullString Or Settings(ArticleConstants.LATEST_ARTICLES_CATEGORIES_EXCLUDE).ToString = "-1") Then
                    Dim categories As String() = Settings(ArticleConstants.LATEST_ARTICLES_CATEGORIES_EXCLUDE).ToString().Split(Char.Parse(","))

                    Dim categoriesToExclude(categories.Length - 1) As Integer
                    For i As Integer = 0 To categories.Length - 1
                        categoriesToExclude(i) = Convert.ToInt32(categories(i))
                    Next

                    If (categories.Length > 0) Then
                    End If

                    catsExclude = categoriesToExclude
                End If
            End If

            Dim count As Integer = 10
            If (Settings.Contains(ArticleConstants.LATEST_ARTICLES_COUNT)) Then
                count = Convert.ToInt32(Settings(ArticleConstants.LATEST_ARTICLES_COUNT).ToString())
            End If

            Dim maxAge As Integer = Null.NullInteger
            If (Settings.Contains(ArticleConstants.LATEST_ARTICLES_MAX_AGE)) Then
                If (Settings(ArticleConstants.LATEST_ARTICLES_MAX_AGE).ToString() <> "") Then
                    maxAge = Convert.ToInt32(Settings(ArticleConstants.LATEST_ARTICLES_MAX_AGE).ToString()) * -1
                End If
            End If

            Dim featuredOnly As Boolean = False
            If (Settings.Contains(ArticleConstants.LATEST_ARTICLES_FEATURED_ONLY)) Then
                featuredOnly = Convert.ToBoolean(Settings(ArticleConstants.LATEST_ARTICLES_FEATURED_ONLY).ToString())
            End If

            If (featuredOnly) Then
            End If

            Dim notFeaturedOnly As Boolean = False
            If (Settings.Contains(ArticleConstants.LATEST_ARTICLES_NOT_FEATURED_ONLY)) Then
                notFeaturedOnly = Convert.ToBoolean(Settings(ArticleConstants.LATEST_ARTICLES_NOT_FEATURED_ONLY).ToString())
            End If



            Dim securedOnly As Boolean = False
            If (Settings.Contains(ArticleConstants.LATEST_ARTICLES_SECURED_ONLY)) Then
                securedOnly = Convert.ToBoolean(Settings(ArticleConstants.LATEST_ARTICLES_SECURED_ONLY).ToString())
            End If



            Dim notSecuredOnly As Boolean = False
            If (Settings.Contains(ArticleConstants.LATEST_ARTICLES_NOT_SECURED_ONLY)) Then
                notSecuredOnly = Convert.ToBoolean(Settings(ArticleConstants.LATEST_ARTICLES_NOT_SECURED_ONLY).ToString())
            End If

            If (notFeaturedOnly) Then
            End If

            Dim sortBy As String = ArticleConstants.DEFAULT_SORT_BY
            If (Settings.Contains(ArticleConstants.LATEST_ARTICLES_SORT_BY)) Then
                sortBy = Settings(ArticleConstants.LATEST_ARTICLES_SORT_BY).ToString()
            End If

            Dim sortDirection As String = ArticleConstants.DEFAULT_SORT_DIRECTION
            If (Settings.Contains(ArticleConstants.LATEST_ARTICLES_SORT_DIRECTION)) Then
                sortDirection = Settings(ArticleConstants.LATEST_ARTICLES_SORT_DIRECTION).ToString()
            End If

            If (Settings.Contains(ArticleConstants.LATEST_ARTICLES_LAYOUT_MODE)) Then
            End If

            Dim itemsPerRow As Integer = ArticleConstants.LATEST_ARTICLES_ITEMS_PER_ROW_DEFAULT
            If (Settings.Contains(ArticleConstants.LATEST_ARTICLES_ITEMS_PER_ROW)) Then
                itemsPerRow = Convert.ToInt32(Settings(ArticleConstants.LATEST_ARTICLES_ITEMS_PER_ROW).ToString())
            End If

            Dim showPending As Boolean = False
            If (Settings.Contains(ArticleConstants.LATEST_ARTICLES_SHOW_PENDING)) Then
                showPending = Convert.ToBoolean(Settings(ArticleConstants.LATEST_ARTICLES_SHOW_PENDING).ToString())
            End If

            Dim authorID As Integer = ArticleConstants.LATEST_ARTICLES_AUTHOR_DEFAULT
            If (Settings.Contains(ArticleConstants.LATEST_ARTICLES_AUTHOR)) Then
                authorID = Convert.ToInt32(Settings(ArticleConstants.LATEST_ARTICLES_AUTHOR).ToString())
            End If

            Dim userIDFilter As String = ""
            If (Settings.Contains(ArticleConstants.LATEST_ARTICLES_QUERY_STRING_FILTER) And Settings.Contains(ArticleConstants.LATEST_ARTICLES_QUERY_STRING_PARAM)) Then
                If (Convert.ToBoolean(Settings(ArticleConstants.LATEST_ARTICLES_QUERY_STRING_FILTER).ToString())) Then
                    authorID = -100
                    Dim param As String = Settings(ArticleConstants.LATEST_ARTICLES_QUERY_STRING_PARAM).ToString()
                    If (param <> "") Then

                    End If
                End If
            End If

            Dim usernameFilter As String = ""

            If (Settings.Contains(ArticleConstants.LATEST_ARTICLES_LOGGED_IN_USER_FILTER)) Then
                If (Convert.ToBoolean(Settings(ArticleConstants.LATEST_ARTICLES_LOGGED_IN_USER_FILTER).ToString())) Then
                    authorID = -100

                End If
            End If

            Dim startDate As DateTime = DateTime.Now
            If (Settings.Contains(ArticleConstants.LATEST_ARTICLES_START_DATE)) Then
                If (Settings(ArticleConstants.LATEST_ARTICLES_START_DATE).ToString() <> "") Then
                    startDate = Convert.ToDateTime(Settings(ArticleConstants.LATEST_ARTICLES_START_DATE).ToString())
                End If
            End If

            Dim bubbleFeatured As Boolean = False
            If (Settings.Contains(ArticleConstants.BUBBLE_FEATURED_ARTICLES)) Then
                bubbleFeatured = Convert.ToBoolean(Settings(ArticleConstants.BUBBLE_FEATURED_ARTICLES).ToString())
                If (bubbleFeatured) Then
                    sortBy = "IsFeatured DESC, " & sortBy
                End If
            End If

            Dim articleIDs As String = ""
            If (Settings.Contains(ArticleConstants.LATEST_ARTICLES_IDS)) Then
                If (Settings(ArticleConstants.LATEST_ARTICLES_IDS).ToString() <> "") Then
                    articleIDs = Settings(ArticleConstants.LATEST_ARTICLES_IDS).ToString()
                End If
            End If

            Dim agedDate As DateTime = Null.NullDate
            If (maxAge <> Null.NullInteger) Then
                If (startDate = Null.NullDate) Then
                    agedDate = DateTime.Now.AddDays(maxAge)
                Else
                    agedDate = startDate.AddDays(maxAge)
                End If
            Else
                If (Settings.Contains(ArticleConstants.LATEST_ARTICLES_MAX_AGE)) Then
                    If (Settings(ArticleConstants.LATEST_ARTICLES_MAX_AGE).ToString() = "1") Then
                        agedDate = startDate.AddDays(-1)
                    End If
                End If
            End If

            Dim tagIDs() As Integer = Nothing
            If (Settings.Contains(ArticleConstants.LATEST_ARTICLES_TAGS)) Then
                Dim objTags As New List(Of Integer)
                Dim objTagController As New TagController
                Dim tags As String = Settings(ArticleConstants.LATEST_ARTICLES_TAGS).ToString()
                If (tags <> "") Then
                    For Each tag As String In tags.Split(","c)
                        objTags.Add(Convert.ToInt32(tag))
                    Next
                    If (objTags.Count > 0) Then
                        tagIDs = objTags.ToArray()
                        Dim rssTags As String = ""
                        For Each tagID As Integer In tagIDs
                            If (rssTags = "") Then
                                rssTags = tagID.ToString()
                            Else
                                rssTags = rssTags & "," & tagID.ToString()
                            End If
                        Next

                    End If
                End If
            End If

            Dim matchOperatorTags As MatchOperatorType = MatchOperatorType.MatchAny
            If (Settings.Contains(ArticleConstants.LATEST_ARTICLES_TAGS_MATCH_OPERATOR)) Then
                matchOperatorTags = CType(System.Enum.Parse(GetType(MatchOperatorType), Settings(ArticleConstants.LATEST_ARTICLES_TAGS_MATCH_OPERATOR).ToString()), MatchOperatorType)
            End If

            Dim matchAllTags As Boolean = False
            If (tagIDs IsNot Nothing) Then
                If (matchOperatorTags = MatchOperatorType.MatchAll) Then
                    matchAllTags = True
                End If
            End If

            Dim doPaging As Boolean = False
            If (Settings.Contains(ArticleConstants.LATEST_ENABLE_PAGER)) Then
                doPaging = Convert.ToBoolean(Settings(ArticleConstants.LATEST_ENABLE_PAGER).ToString())
            End If

            Dim pageSize As Integer = count
            If (doPaging) Then
                If (Settings.Contains(ArticleConstants.LATEST_PAGE_SIZE)) Then
                    pageSize = Convert.ToInt32(Settings(ArticleConstants.LATEST_PAGE_SIZE).ToString())
                Else
                    pageSize = ArticleConstants.LATEST_PAGE_SIZE_DEFAULT
                End If
            End If

            'Récupération des articles selon paramètrage
            Dim articleCount As Integer = 0
            Dim objArticles As List(Of ArticleInfo) = objArticleController.GetArticleList(articleModuleID, startDate, agedDate, cats, matchAll, catsExclude, count, pageNumber, pageSize, sortBy, sortDirection, True, False, Null.NullString, authorID, showPending, False, featuredOnly, notFeaturedOnly, securedOnly, notSecuredOnly, articleIDs, tagIDs, matchAllTags, Null.NullString, Null.NullInteger, Null.NullString, Null.NullString, articleCount)

            If objArticles.Count > 0 Then
                For Each article As ArticleInfo In objArticles
                    If article.IsApproved Then
                        Dim searchItem As SearchItemInfo = New SearchItemInfo(article.Title, article.Summary, article.AuthorID, article.LastUpdate, ModInfo.ModuleID, article.ArticleID.ToString, article.Body, ("ArticleType=ArticleView&ArticleID=" & article.ArticleID.ToString))
                        searchItemsCollect.Add(searchItem)
                    End If

                Next
            End If

            Return searchItemsCollect

        End Function
#End Region

    End Class
End Namespace
